package cn.ekgc.carddemo.service;

import cn.ekgc.carddemo.entity.Pagination;
import cn.ekgc.carddemo.entity.Student;
import cn.ekgc.carddemo.entity.StudentExample;
import cn.ekgc.carddemo.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public void save(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public void delete(int id) {
        studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(StudentExample studentExample) {
        studentMapper.deleteByExample(studentExample);
    }

    @Override
    public List<Student> list(StudentExample studentExample) {
        return studentMapper.selectByExample(studentExample);
    }

    @Override
    public Map<String, Object> list(Pagination pagination) {
        //怎么让我们的mybatis做分页
        PageHelper.startPage(pagination.getPageNum(),pagination.getPageSize());
        StudentExample example = new StudentExample();
        //回头补充
        List<Student> students = studentMapper.selectByExample(example);
        PageInfo<Student> pageInfo= new PageInfo<>(students);
        Map<String,Object> data=new HashMap<>();
        data.put("total",pageInfo.getTotal());
        data.put("rows",students);
        return data;
    }
}
